﻿//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 作 者：曾洋
// 日 期：2012-1-3 16:11:29
// 描 述：约场记录信息处理
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00             
//==================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.Common;
using ZX.DBModule;
using ZX.BLL.Extend;
using System.Text;
using ZX.EHR.Library;
using ZX.CRM.Bussiness;
using ZX.SYS.Library;
using ZX.CRM.Library;
using ZX.SYS.Bussiness.Message;
using ZX.BLL;
using ZX.Web.UI.Controls.ControlBind;

namespace ZX.WebApp.Crm.Customer
{
    public partial class EditCrmActivity : EditUIPage
    {  
        BTCrmCustomer bll = new BTCrmCustomer();
        protected void Page_Load(object sender, EventArgs e)
        {
            #region 加载
            AjaxPro.Utility.RegisterTypeForAjax(typeof(ZX.WebApp.Crm.Customer.EditCrmActivity));
            if (!this.IsPostBack)
            {
                bindTime();

                //编辑信息
                this.ID.Value = Guid.NewGuid().ToString();
                string strId = this.GetStringFromQueryString("id", "");
                string cid = this.GetStringFromQueryString("cid", "");
                if (cid != null && cid != "")
                {

                    this.CustomId.Value = new Guid(cid).ToString();
                    if (strId != null && strId != "")
                    {
                        V_CrmActivityTel cci = bll.GetTelInfo(new Guid(strId));
                        if (cci != null)
                        {
                            txtCustomId.Value = cci.CustomerName;
                            IList<V_CrmLinkMan> lvc = bll.GetLinkManInfoByCustomerId(new Guid(cid));
                            if (lvc != null && lvc.Count > 0)
                            {
                                ddlLinkMan.DataSource = lvc;
                                ddlLinkMan.DataValueField = "ID";
                                ddlLinkMan.DataTextField = "Caption";
                                ddlLinkMan.DataBind();
                            }
                           
                            txtCaption.Value = cci.Suject;
                            txtPhone.Text = cci.MobileTel;

                            ID.Value = strId;
                            txtCallContent.Text = cci.Contents;
                            txtExplain.Text = cci.Explain;
                            FactStartDate.Value = cci.PlanStartDate.ToString("yyyy-MM-dd");
                            FactEndDate.Value = cci.PlanEndDate.ToString("yyyy-MM-dd");
                            string fsm = cci.PlanStartDate.Hour < 10 ? "0" + cci.PlanStartDate.Hour.ToString() : cci.PlanStartDate.Hour.ToString();
                            string fss = cci.PlanStartDate.Minute < 10 ? "0" + cci.PlanStartDate.Minute.ToString() : cci.PlanStartDate.Minute.ToString();
                            string fem = cci.PlanEndDate.Hour < 10 ? "0" + cci.PlanEndDate.Hour.ToString() : cci.PlanEndDate.Hour.ToString();
                            string fes = cci.PlanEndDate.Minute < 10 ? "0" + cci.PlanEndDate.Minute.ToString() : cci.PlanEndDate.Minute.ToString();
                            ddlStartMin.SelectedValue = fsm;
                            ddlStartSec.SelectedValue = fss;
                            ddlEndMin.SelectedValue = fem;
                            ddlEndSec.SelectedValue = fes;
                            ddlAnimalType.SelectedValue = cci.TypeId.ToString().ToUpper();
                            ddlLinkMan.SelectedValue = cci.LinkManID.ToString();
                        }
                    }
                    else {
                        IList<V_CrmLinkMan> lvc = bll.GetLinkManInfoByCustomerId(new Guid(cid));
                        if (lvc != null && lvc.Count > 0)
                        {
                            ddlLinkMan.DataSource = lvc;
                            ddlLinkMan.DataValueField = "ID";
                            ddlLinkMan.DataTextField = "Caption";
                            ddlLinkMan.DataBind();
                            txtCustomId.Value = lvc[0].CustomerName;
                            txtPhone.Text = lvc[0].MobileTel;
                        }
                    }

                }
            }
            #endregion
        }
        public string strEndDate
        {
            get { return DateTime.Now.ToString("yyyy-MM-dd");}
        }
        private void bindTime()
        {
            BDatPhraseDetail bllDetail = new BDatPhraseDetail();
            this.FactStartDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
            this.FactEndDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
           
            IList<DatPhraseDetail> gddb = bllDetail.GetDatPhraseDetailByClassValue("TelAnimal");  
            DropDownListControl.DropDataSourceBind(this.ddlAnimalType, gddb, "PhraseText", "PhraseID");
            
            for (int i = 0; i < 24; i++)
            {
                ListItem itemHour = new ListItem();
                itemHour.Text = i < 10 ? "0" + i.ToString() : i.ToString();
                itemHour.Value = i < 10 ? "0" + i.ToString() : i.ToString();
                ddlStartMin.Items.Add(itemHour);
            }
            for (int i = 0; i < 60; i++)
            {
                ListItem itemMinute = new ListItem();
                itemMinute.Text = i < 10 ? "0" + i.ToString() : i.ToString();
                itemMinute.Value = i < 10 ? "0" + i.ToString() : i.ToString();
                ddlStartSec.Items.Add(itemMinute);
            }
            for (int i = 0; i < 24; i++)
            {
                ListItem itemHour = new ListItem();
                itemHour.Text = i < 10 ? "0" + i.ToString() : i.ToString();
                itemHour.Value = i < 10 ? "0" + i.ToString() : i.ToString();
                ddlEndMin.Items.Add(itemHour);
            }
            for (int i = 0; i < 60; i++)
            {
                ListItem itemMinute = new ListItem();
                itemMinute.Text = i < 10 ? "0" + i.ToString() : i.ToString();
                itemMinute.Value = i < 10 ? "0" + i.ToString() : i.ToString();
                ddlEndSec.Items.Add(itemMinute);
            }
            for (int i = 0; i < 24; i++)
            {
                ListItem itemHour = new ListItem();
                itemHour.Text = i < 10 ? "0" + i.ToString() : i.ToString();
                itemHour.Value = i < 10 ? "0" + i.ToString() : i.ToString();
                ddlHour.Items.Add(itemHour);
            }
            for (int i = 0; i < 60; i++)
            {
                ListItem itemMinute = new ListItem();
                itemMinute.Text = i < 10 ? "0" + i.ToString() : i.ToString();
                itemMinute.Value = i < 10 ? "0" + i.ToString() : i.ToString();
                ddlMinute.Items.Add(itemMinute);
            }
        }

        //提交消息提醒
        [AjaxPro.AjaxMethod]
        public string Save(object msgTitle, object msgContent, object planTime)
        {
            try
            {
                DateTime dt = DateTime.Now;
                DateTime meSend = Convert.ToDateTime(planTime);
                if (meSend > dt)
                {
                    BDatMessageQueue target = new BDatMessageQueue(); // TODO: 初始化为适当的值
                    DatMessageQueue msg = DatMessageQueue.New(); // TODO: 初始化为适当的值
                    msg.MsgID = Guid.NewGuid();
                    msg.Title = msgTitle.ToString();
                    msg.MsgTemplate = "xxxx";
                    msg.MsgContent = msgContent.ToString();
                    msg.MessageType = "0";
                    msg.ExpectTime = meSend;
                    msg.AddUser = (Guid)this.UserId;
                    msg.PushTime = DateTime.Now;
                    msg.QueryID = new Guid("F8ED27B4-B5BC-4497-9881-FE3AD0512125");//关联信息来源的编号

                    IList<DatMessageReceiver> MessageReceiver = new List<DatMessageReceiver>(); // TODO: 初始化为适当的值
                    DatMessageReceiver mr1 = DatMessageReceiver.New();
                    mr1.MsItemID = Guid.NewGuid();
                    mr1.MsgID = msg.MsgID;
                    mr1.ReceiverID = (Guid)this.UserId;
                    mr1.ReceiverType = "U";
                    MessageReceiver.Add(mr1);
                    target.AddMessage(msg, MessageReceiver);
                    BDatMessageQueue target1 = new BDatMessageQueue(); // TODO: 初始化为适当的值
                    DateTime CurrentTime = DateTime.Now; // TODO: 初始化为适当的值
                    target1.PushMessages(CurrentTime); return "1";
                } return "0";
            }
            catch (Exception)
            {
                return "0";
            }
        }
    }
}